﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;

namespace ProjectMain
{
    public partial class FrmModifyPassword : Form
    {
        Boolean InputRight;
        string cOldPasswordMD5;
        ProjectCommon.ClassCommon cc;

        public FrmModifyPassword(ProjectCommon.ClassCommon aClassCommon)
        {
            InitializeComponent();
            cc = aClassCommon;
            cOldPasswordMD5 = aClassCommon.dtLoginUser.Rows[0]["Password"].ToString();
            InputRight=false;
            lblPasswordOldInfo.BackColor = SystemColors.Control;
            lblPasswordNewInfo.BackColor = SystemColors.Control;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (InputRight == true)
            {
                CtrlEncryptor.DESEncryptor fDESEncryptor = new CtrlEncryptor.DESEncryptor();
                fDESEncryptor.InputString = txtPasswordOld.Text;
                fDESEncryptor.MD5Encrypt();
                if (cOldPasswordMD5 == fDESEncryptor.OutString)
                {
                    fDESEncryptor.InputString = txtPasswordConfirm.Text;
                    fDESEncryptor.MD5Encrypt();
                    cc.dtLoginUser.Rows[0]["Password"] = fDESEncryptor.OutString;
                    string id = cc.dtLoginUser.Rows[0]["ID"].ToString();
                    string sql = @"UPDATE [kengee_it_mis].[dbo].[LoginUser]
                                       SET [Password] = '" + fDESEncryptor.OutString + @"'
                                     WHERE ID='" + id + "'";
                    sql = ProjectCommon.ClassCommon.ToDBC(sql);
                    SqlCommand scm = new SqlCommand(sql,cc.scKengee_IT_MIS);
                    cc.scKengee_IT_MIS.Open();
                    scm.ExecuteNonQuery();
                    cc.scKengee_IT_MIS.Close();
                    scm.Dispose();
                    this.Close();
                    
                }
                else
                {
                    lblPasswordOldInfo.Text = "旧密码输入错误！";
                    lblPasswordOldInfo.ForeColor = Color.Red;
                }
            }
           
        }

        private void txtPasswordNew_TextChanged(object sender, EventArgs e)
        {
            if (txtPasswordNew.Text.Length < 6)
            {
                lblPasswordNewInfo.ForeColor = Color.Red;
                lblPasswordNewInfo.Text = "密码长度太短";
                InputRight = false;
            }
            else if (txtPasswordNew.Text != txtPasswordConfirm.Text)
            {
                lblPasswordNewInfo.ForeColor = Color.Red;
                lblPasswordNewInfo.Text = "确认密码不一致！";
                InputRight = false;
            }
            else
            {
                InputRight = true;
                lblPasswordNewInfo.Text = "";
            }
        }

        
    }
}